package com.winderinfo.yikaotianxia.ui.classification;


import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.ui.home.ZhuanYeBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class ClassificationFragment extends BaseFragment implements Contract.View {

    @BindView(R.id.cf_tab)
    TabLayout cfTab;
    @BindView(R.id.cf_pager)
    ViewPager cfPager;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();

    @Override
    protected int initLayoutId() {
        return R.layout.fragment_classification;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);

    }

    @Override
    protected void initData() {
        //请求专业数据
        /**
         * @pageNum 页码
         * @pageSize 每页数量
         * */
        map.clear();
        map.put("pageNum", 1);
        map.put("pageSize", 4);
        presenter.onPostRequest(Api.HOME_ZHUANYE, map, ZhuanYeBean.class);
    }


    @Override
    public void onSuccess(Object data) {
        //专业数据
        if (data instanceof ZhuanYeBean) {
            List<ZhuanYeBean.RowsBean> rows = ((ZhuanYeBean) data).getRows();
            //设置适配器
            BasePagerAdapter basePagerAdapter = new BasePagerAdapter(getChildFragmentManager(), rows);
            cfPager.setAdapter(basePagerAdapter);
            cfTab.setupWithViewPager(cfPager);
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
